import _ from 'lodash';
import {query} from '../services/scanQrcode';
import createModel from './base';
import {isSuccess} from '../configs/error';


export default createModel({
  namespace: 'scanQrcode',

  state: {
    canTest: false
  },

  subscriptions: {
    setup({dispatch, history}) {
      history.listen(location => {
        if (location.pathname === '/scanQrcode') { // 每次刷新一下，获取最新
          dispatch({
            type: 'queryPro'
          });
        }
      });
    },
  },

  effects: {
    * queryPro(state, {put, call}) {
      const {payload} = state
      const data = yield call(query, payload);
      console.log(state)
      if (isSuccess(data)) {
        localStorage.setItem('bindProInfo',JSON.stringify(data.data))
        yield put({
          type: 'querySuccess',
          payload: {...data.data, canTest: true},
        });
      } else {
        localStorage.setItem('bindProInfo',JSON.stringify({}))
        yield put({ type: 'login/hideLoginLoading' });
        // throw data;
      }
    },
  },
  reducers: {
    querySuccess(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    },
  },
}, query);
